//
//  YCNoticeView.m
//  lianganyuchu
//
//  Created by iMac on 17/5/22.
//  Copyright © 2017年 杭州桑缇电子商务有限公司. All rights reserved.
//

#import "YCNoticeView.h"

@interface YCNoticeView()
{
    UILabel *m_labTitle;
    
    UIButton *m_btnClose;
    UIWebView *m_webView;
}
@end
@implementation YCNoticeView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubviewsWith:frame];
    }
    return self;
}

-(void)addSubviewsWith:(CGRect)frame
{
    UIView *backView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, kMainScreen_Width, kMainScreen_Height)];
    [backView setBackgroundColor:[UIColor blackColor]];
    [backView setAlpha:0.3];
    [backView click:self action:@selector(hide)];
    [self addSubview:backView];
    
    UIView *viewCenter=[[UIView alloc]init];
    [viewCenter setBackgroundColor:WHITE_COLOR];
    [self addSubview:viewCenter];
    CGFloat height=(kMainScreen_Width-80)*43/32;
    [viewCenter mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(40);
        make.right.equalTo(self).offset(-40);
        make.centerY.equalTo(self.centerY);
        make.height.mas_equalTo(height);
    }];
    viewCenter.layer.cornerRadius=4.0f;
    [viewCenter.layer addSublayer:getLine(0, kMainScreen_Width-40, 50, 50, LINE_COLOR)];
    
    m_labTitle=[[UILabel alloc]init];
    [m_labTitle setFont:[UIFont FontOfSize:18]];
    [m_labTitle setTextColor:TEXT_DEEP];
    [m_labTitle setTextAlignment:NSTextAlignmentCenter];
    [viewCenter addSubview:m_labTitle];
    [m_labTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(0);
        make.right.equalTo(self).offset(0);
        make.top.equalTo(viewCenter).offset(15);
        make.height.mas_equalTo(20);
    }];
    
    
    m_btnClose=[[UIButton alloc]init];
    [m_btnClose setImage:[UIImage imageNamed:@"notice_close"] forState:UIControlStateNormal];
    [m_btnClose addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
    [m_btnClose setImageEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
    [self addSubview:m_btnClose];
    [m_btnClose mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(viewCenter.bottom).offset(10);
        make.centerX.equalTo(self.centerX);
        make.width.mas_equalTo(45);
        make.height.mas_equalTo(45);
    }];
    
    m_webView=[[UIWebView alloc]init];
    [m_webView setBackgroundColor:WHITE_COLOR];
    m_webView.scrollView.bounces=NO;
    [viewCenter addSubview:m_webView];
    [m_webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(viewCenter).offset(50);
        make.bottom.equalTo(viewCenter).offset(-5);
        make.left.equalTo(viewCenter).offset(0);
        make.right.equalTo(viewCenter).offset(0);
    }];
    
}
-(void)reloadWith:(ShopTable *)shop
{
    [m_labTitle setText:shop.notice_title];
    
    if (shop.notice_type.intValue==1) {
        NSString *newBacnStr = [shop.notice_content stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [m_webView loadHTMLString:newBacnStr baseURL:nil];
    }else{
        [m_webView loadHTMLString:shop.notice_content baseURL:nil];
    }
    
}
-(void)show
{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    self.alpha = 0;
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1.0;
    } completion:^(BOOL finished) {
    }];
}
-(void)hide
{
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}







@end


















